package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.TWard;
import com.ruoyi.system.domain.vo.TBedVo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ITWardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TBed;
import com.ruoyi.system.service.ITBedService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 床位信息Controller
 * 
 * @author 杨一迪
 * @date 2024-12-28
 */
@Controller
@RequestMapping("/system/bed")
public class TBedController extends BaseController
{
    private String prefix = "system/bed";

    @Autowired
    private ITWardService tWardService;

    @Resource
    private ISysDeptService sysDeptService;

    @Autowired
    private ITBedService tBedService;

    @RequiresPermissions("system:bed:view")
    @GetMapping()
    public String bed()
    {
        return prefix + "/bed";
    }

    /**
     * 查询床位信息列表
     */
    @RequiresPermissions("system:bed:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TBed tBed)
    {
        startPage();
        List<TBed> list = tBedService.selectTBedList(tBed);
        List<TBedVo> bedVos = new ArrayList<>();
        list.forEach(bed -> {
            TBedVo tBedVo = new TBedVo();
            BeanUtils.copyProperties(bed,tBedVo);
            TWard tWard = tWardService.selectTWardById(bed.getWardId());
            tBedVo.setWardNumber(tWard.getWardNumber());
            bedVos.add(tBedVo);
        });
        return getDataTable(bedVos);
    }

    /**
     * 导出床位信息列表
     */
    @RequiresPermissions("system:bed:export")
    @Log(title = "床位信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TBed tBed)
    {
        List<TBed> list = tBedService.selectTBedList(tBed);
        ExcelUtil<TBed> util = new ExcelUtil<TBed>(TBed.class);
        return util.exportExcel(list, "床位信息数据");
    }

    /**
     * 新增床位信息
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        List<TWard> tWards = tWardService.selectTWardList(new TWard());
        mmap.put("wards",tWards);
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        mmap.put("depts",sysDepts);
        return prefix + "/add";
    }

    /**
     * 新增保存床位信息
     */
    @RequiresPermissions("system:bed:add")
    @Log(title = "床位信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TBed tBed)
    {
        return toAjax(tBedService.insertTBed(tBed));
    }

    /**
     * 修改床位信息
     */
    @RequiresPermissions("system:bed:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TBed tBed = tBedService.selectTBedById(id);
        mmap.put("tBed", tBed);
        List<TWard> tWards = tWardService.selectTWardList(new TWard());
        mmap.put("wards",tWards);
        List<SysDept> sysDepts = sysDeptService.selectDeptList(new SysDept());
        mmap.put("depts",sysDepts);
        return prefix + "/edit";
    }

    /**
     * 修改保存床位信息
     */
    @RequiresPermissions("system:bed:edit")
    @Log(title = "床位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TBed tBed)
    {
        return toAjax(tBedService.updateTBed(tBed));
    }

    /**
     * 删除床位信息
     */
    @RequiresPermissions("system:bed:remove")
    @Log(title = "床位信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tBedService.softDeleteTBedByIds(ids));
    }

    @Log(title = "床位信息", businessType = BusinessType.UPDATE)
    @PostMapping("/selectBedByWardId")
    @ResponseBody
    public AjaxResult selectBed(String wardId,String patientId)
    {
        return AjaxResult.success(tBedService.selectBedByWardId(wardId, patientId));
    }
}
